package info.wangyuan.agent.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import info.wangyuan.agent.common.Enum.SensitiveType;

import java.io.IOException;

/**
 * 脱敏序列化器
 */
public class SensitiveSerializer extends JsonSerializer<String> {

    private final SensitiveType type;

    public SensitiveSerializer(SensitiveType type) {
        this.type = type;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value == null) {
            gen.writeNull();
            return;
        }
        switch (type) {
            case EMAIL -> gen.writeString(DesensitizeUtil.maskEmail(value));
            case API_KEY -> gen.writeString(DesensitizeUtil.maskApiKey(value));
            case OPEN_ID -> gen.writeString(DesensitizeUtil.maskWeChatOpenId(value));
            default -> gen.writeString(value);
        }
    }
}
